package com.jzqf.android.alive.navi

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.Window
import android.view.WindowManager
import androidx.fragment.app.FragmentActivity
import com.baidu.navisdk.adapter.BNaviCommonParams
import com.baidu.navisdk.adapter.BaiduNaviManagerFactory
import com.baidu.navisdk.adapter.IBNRouteGuideManager
import com.baidu.navisdk.adapter.IBNaviListener
import com.baidu.navisdk.adapter.IBNaviViewListener
import com.baidu.navisdk.adapter.struct.BNGuideConfig

class NaviGuideActivity : FragmentActivity() {
    private val TAG: String = NaviGuideActivity::class.java.getName()

    private var mRouteGuideManager: IBNRouteGuideManager? = null
    private val mMode = IBNaviListener.DayNightMode.DAY

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val fullScreen = supportFullScreen()
        val params = Bundle()

        params.putBoolean(BNaviCommonParams.ProGuideKey.IS_SUPPORT_FULL_SCREEN, fullScreen)
        mRouteGuideManager = BaiduNaviManagerFactory.getRouteGuideManager()
        val config = BNGuideConfig.Builder().params(params).build()
        val view = mRouteGuideManager!!.onCreate(this, config)

        if (view != null) {
            setContentView(view)
        }
        routeGuideEvent()
    }

    // 导航过程事件监听
    private fun routeGuideEvent() {
        BaiduNaviManagerFactory.getRouteGuideManager().setNaviListener(object : IBNaviListener() {
            override fun onNaviGuideEnd() {
                this@NaviGuideActivity.finish()
            }

            override fun onYawingSuccess() {
                super.onYawingSuccess()
                Log.e(TAG, "onYawingSuccess")
                val routePlanInfo = BaiduNaviManagerFactory.getRoutePlanManager().getRoutePlanInfo()
            }
        })

        BaiduNaviManagerFactory.getRouteGuideManager()
            .setNaviViewListener(object : IBNaviViewListener() {
                override fun onMainInfoPanCLick() {
                }

                override fun onNaviTurnClick() {
                }

                override fun onFullViewButtonClick(b: Boolean) {
                }

                override fun onFullViewWindowClick(b: Boolean) {
                }

                override fun onNaviBackClick() {
                }

                override fun onBottomBarClick(action: Action?) {
                }

                override fun onNaviSettingClick() {
                }

                override fun onRefreshBtnClick() {
                }

                override fun onZoomLevelChange(i: Int) {
                }

                override fun onMapClicked(v: Double, v1: Double) {
                }

                override fun onMapMoved() {
                    Log.e(TAG, "onMapMoved")
                }

                override fun onFloatViewClicked() {
                    try {
                        val intent = Intent()
                        intent.setPackage(getPackageName())
                        intent.setClass(
                            this@NaviGuideActivity,
                            Class.forName(NaviGuideActivity::class.java.getName())
                        )
                        intent.setFlags(
                            Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                        )
                        startActivity(intent)
                    } catch (e: ClassNotFoundException) {
                        e.printStackTrace()
                    }
                }
            })
    }

    override fun onStart() {
        super.onStart()
        mRouteGuideManager!!.onStart()
    }

    override fun onResume() {
        super.onResume()
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
        mRouteGuideManager!!.onResume()
    }

    override fun onPause() {
        super.onPause()
        mRouteGuideManager!!.onPause()
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
    }

    override fun onStop() {
        super.onStop()
        mRouteGuideManager!!.onStop()
    }

    override fun onDestroy() {
        super.onDestroy()
        mRouteGuideManager!!.onDestroy(false)
        mRouteGuideManager = null
    }

    @SuppressLint("MissingSuperCall")
    override fun onBackPressed() {
        mRouteGuideManager!!.onBackPressed(false, true)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        mRouteGuideManager!!.onConfigurationChanged(newConfig)
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (!mRouteGuideManager!!.onKeyDown(keyCode, event)) {
            return super.onKeyDown(keyCode, event)
        }
        return true
    }

    private fun supportFullScreen(): Boolean {
        if (Build.VERSION.SDK_INT >= 21) {
            val window: Window = getWindow()
            val color: Int
            if (Build.VERSION.SDK_INT >= 23) {
                color = Color.TRANSPARENT
            } else {
                color = 0x2d000000
            }
            window.setStatusBarColor(color)

            if (Build.VERSION.SDK_INT >= 23) {
                window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                )
                var uiVisibility = window.getDecorView().getSystemUiVisibility()
                if (mMode == IBNaviListener.DayNightMode.DAY) {
                    uiVisibility = uiVisibility or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                }
                window.getDecorView().setSystemUiVisibility(uiVisibility)
            } else {
                window.getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                )
            }

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            return true
        }

        return false
    }

    override fun onRequestPermissionsResult(
        requestCode: Int, permissions: Array<out String?>, grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        mRouteGuideManager!!.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        mRouteGuideManager!!.onActivityResult(requestCode, resultCode, data)
    }
}